/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pro.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UploadUtils;
import com.thinkgem.jeesite.modules.pro.entity.ProjectTask;
import com.thinkgem.jeesite.modules.pro.entity.TaskGroup;
import com.thinkgem.jeesite.modules.pro.service.ProjectTaskService;
import com.thinkgem.jeesite.modules.pro.service.TaskGroupService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 任务管理Controller
 * @author fanqinglong
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/pro/projectTask")
public class ProjectTaskController extends BaseController {

	@Autowired
	private ProjectTaskService projectTaskService;
	@Autowired
	private TaskGroupService taskGroupService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public ProjectTask get(@RequestParam(required=false) String id) {
		ProjectTask entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectTaskService.get(id);
		}
		if (entity == null){
			entity = new ProjectTask();
		}
		return entity;
	}
	
	@RequiresPermissions("pro:projectTask:view")
	@RequestMapping(value = {"index"})
	public String index(){
		return "modules/pro/taskIndex";
	}
	
	@RequiresPermissions("pro:projectTask:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectTask projectTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectTask> page = projectTaskService.findPage(new Page<ProjectTask>(request, response), projectTask); 
		model.addAttribute("page", page);
		TaskGroup taskGroup = new TaskGroup();
		if (null != projectTask.getTaskGroup()) {
			taskGroup = taskGroupService.get(projectTask.getTaskGroup().getId());
		}
		projectTask.setProject(taskGroup.getProject());
		model.addAttribute("groups", taskGroupService.findList(taskGroup));
		model.addAttribute("e", projectTask);
		return "modules/pro/projectTaskList";
	}

	@RequiresPermissions("pro:projectTask:view")
	@RequestMapping(value = "form")
	public String form(ProjectTask projectTask, Model model) {
		TaskGroup taskGroup = new TaskGroup();
		String projectId = "";
		if (null != projectTask.getTaskGroup()) {
			taskGroup = taskGroupService.get(projectTask.getTaskGroup().getId());
			projectId = taskGroup.getProject().getId();
			if (null != taskGroup.getProject()) {
				projectTask.setProject(taskGroup.getProject());
			}
		}
		model.addAttribute("groups", taskGroupService.findList(taskGroup));
		model.addAttribute("projectTask", projectTask);
		// 查询出部门
		Office officeParam = new Office();
		officeParam.setType("2");
		List<Office> officeList = officeService.findListForProject(officeParam);
		model.addAttribute("officeList", officeList);
		// 查询出该项目下人员
		List<User> userList = systemService.getByProjectId(projectId);
		model.addAttribute("userList", userList);
		model.addAttribute("projectId", projectId);
		return "modules/pro/projectTaskForm";
	}
	@RequiresPermissions("pro:projectTask:view")
	@RequestMapping(value = "formDisable")
	public String formDisable(ProjectTask projectTask, Model model) {
		TaskGroup taskGroup = new TaskGroup();
		if (null != projectTask.getTaskGroup()) {
			taskGroup = taskGroupService.get(projectTask.getTaskGroup().getId());
		}
		model.addAttribute("groups", taskGroupService.findList(taskGroup));
		model.addAttribute("projectTask", projectTask);
		return "modules/pro/projectTaskFormDisable";
	}

	@RequiresPermissions("pro:projectTask:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(HttpServletRequest request,ProjectTask projectTask, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, projectTask)){
			return form(projectTask, model);
		}
		projectTaskService.save(request, projectTask);
		addMessage(redirectAttributes, "保存任务成功");
		//return "redirect:"+Global.getAdminPath()+"/pro/projectTask/?repage&project.id="+projectTask.getProject().getId()+"&taskGroup.id="+projectTask.getTaskGroup().getId();
		return "success";
	}
	
	@RequiresPermissions("pro:projectTask:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectTask projectTask, RedirectAttributes redirectAttributes) {
		projectTaskService.delete(projectTask);
		addMessage(redirectAttributes, "归档任务成功");
		return "redirect:"+Global.getAdminPath()+"/pro/projectTask/?repage&project.id="+projectTask.getProject().getId()+"&taskGroup.id="+projectTask.getTaskGroup().getId();
	}
	
	@RequiresPermissions("pro:projectTask:edit")
	@RequestMapping(value = "deleteForWorkPanel")
	@ResponseBody
	public String deleteForWorkPanel(ProjectTask projectTask) {
		projectTaskService.delete(projectTask);
		return "success";
	}

	@RequiresPermissions("pro:projectTask:edit")
	@RequestMapping(value = "upload")
	@ResponseBody
	public String upload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
		response.setContentType("text/html;charset=UTF-8");
		String[] info = new UploadUtils().uploadFile(request,file);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", info[1]);
		jsonObject.put("name", info[2]);
		jsonObject.put("realName", info[4].substring(info[4].lastIndexOf("/")+1));
		return jsonObject.toString();
	}
	
	@RequiresPermissions("pro:projectTask:edit")
	@RequestMapping(value = "getByName")
	@ResponseBody
	public String getByName(HttpServletRequest request,ProjectTask projectTask) {
		String result = "false";
		String taskGroupId = request.getParameter("taskGroupId");
		projectTask.setTaskGroup(new TaskGroup(taskGroupId));
		ProjectTask resultTask = projectTaskService.getByName(projectTask);
		if (null == resultTask || resultTask.getId().equals(projectTask.getId())) {
			result = "true";
		}
		return result;
	}
	
	
	@RequestMapping(value = "updateFromGantt")
	@ResponseBody
	public String updateFromGantt(ProjectTask projectTask,HttpServletRequest request){
		if (!SecurityUtils.getSubject().isPermitted("pro:projectTask:edit")) {
			return "noAuth";
		}
		projectTaskService.updateFromGantt(projectTask);
		return "success";
	}
}